//
//  MarryViewController.m
//  Liuhe
//
//  Created by zr on 2018/8/17.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "MarryViewController.h"

@interface MarryViewController ()

@property (nonatomic, weak)UIView * topView;
@property (nonatomic, weak)UIView * bottomView;
@property (nonatomic, weak)UIButton * sureButton;
@property (nonatomic, weak)UIImageView * manIcon;
@property (nonatomic, weak)UIImageView * womanIcon;
@property (nonatomic, strong)UILabel * manNameLabel;
@property (nonatomic, strong)UILabel * manBriLabel;
@property (nonatomic, strong)UILabel * manTimeLabel;
@property (nonatomic, strong)UILabel * womanNameLabel;
@property (nonatomic, strong)UILabel * womanBriLabel;
@property (nonatomic, strong)UILabel * womanTimeLabel;
@property (nonatomic, strong)UITextField * manNameText;
@property (nonatomic, strong)UITextField * manBriText;
@property (nonatomic, strong)UITextField * manTimeText;
@property (nonatomic, strong)UITextField * womanNameText;
@property (nonatomic, strong)UITextField * womanBriText;
@property (nonatomic, strong)UITextField * womanTimeText;

@end

@implementation MarryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = WHITE_COLOR;
    
    [self setupNav];
    [self setupUI];
    
}


- (void)setupNav{
    
    self.title = @"八字合婚";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(0, 0, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"1返回"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
    
}

- (void)backToView{
    
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)setupUI{
    
    UIView * topView = [UIView new];
    
    topView.backgroundColor = MAINCOLOR;
    topView.layer.cornerRadius = 9;
    topView.userInteractionEnabled = YES;
    
    self.topView = topView;
    
    [self.view addSubview:self.topView];
    
    UIImageView * manIcon = [UIImageView new];
    
    manIcon.image = ImageNamed(@"gvfdg12");
    
    self.manIcon = manIcon;
    
    [self.topView addSubview:self.manIcon];
    
    
    self.manNameLabel = [self setupLabelWith:@"男方姓名:"];
    [self.topView addSubview:self.manNameLabel];
    self.manBriLabel = [self setupLabelWith:@"出生日期:"];
    [self.topView addSubview:self.manBriLabel];
    self.manTimeLabel = [self setupLabelWith:@"出生日期:"];
    [self.topView addSubview:self.manTimeLabel];
    
    self.manNameText = [self setupTextFiledWith:@"请输入汉字"];
    [self.topView addSubview:self.manNameText];
    self.manBriText = [self setupTextFiledWith:@"日期:1990-03-05"];
    [self.topView addSubview:self.manBriText];
    self.manTimeText = [self setupTextFiledWith:@"时间:10"];
    [self.topView addSubview:self.manTimeText];
    
    UIView * bottomView = [UIView new];
    
    bottomView.backgroundColor = MAINCOLOR;
    bottomView.layer.cornerRadius = 9;
    bottomView.userInteractionEnabled = YES;
    
    self.bottomView = bottomView;
    
    [self.view addSubview:self.bottomView];
    
    
    UIImageView * womanIcon = [UIImageView new];
    
    womanIcon.image = ImageNamed(@"sadsad5415");
    
    self.womanIcon = womanIcon;
    
    [self.bottomView addSubview:self.womanIcon];
    
    
    self.womanNameLabel = [self setupLabelWith:@"女方姓名:"];
    [self.bottomView addSubview:self.womanNameLabel];
    self.womanBriLabel = [self setupLabelWith:@"出生日期:"];
    [self.bottomView addSubview:self.womanBriLabel];
    self.womanTimeLabel = [self setupLabelWith:@"出生日期:"];
    [self.bottomView addSubview:self.womanTimeLabel];

    self.womanNameText = [self setupTextFiledWith:@"请输入汉字"];
    [self.bottomView addSubview:self.womanNameText];
    self.womanBriText = [self setupTextFiledWith:@"日期:1990-03-05"];
    [self.bottomView addSubview:self.womanBriText];
    self.womanTimeText = [self setupTextFiledWith:@"时间:10"];
    [self.bottomView addSubview:self.womanTimeText];
    
    UIButton * sureButton = [UIButton new];
    
    [sureButton addTarget:self action:@selector(clickSureButton) forControlEvents:UIControlEventTouchUpInside];
    [sureButton setImage:ImageNamed(@"desfds55") forState:UIControlStateNormal];
    
    self.sureButton = sureButton;
    
    [self.view addSubview:self.sureButton];
}


- (void)viewDidLayoutSubviews{
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(12 * SCREENH6);
        make.trailing.mas_equalTo(self.view).offset(-12 * SCREENH6);
        make.top.mas_equalTo(self.view).offset(30 * SCREENH6);
        make.height.mas_equalTo(185 * SCREENH6);
    }];
    
    [self.manIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.topView).offset(8 * SCREENH6);
        make.centerY.mas_equalTo(self.topView);
        make.width.height.mas_equalTo(67 * SCREENH6);
    }];
    
    [self.manNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.manIcon.mas_trailing).offset(10 * SCREENH6);
        make.top.mas_equalTo(self.topView).offset(20 * SCREENH6);
    }];
    
    [self.manBriLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.manNameLabel);
        make.top.mas_equalTo(self.manNameLabel.mas_bottom).offset(30 * SCREENH6);
    }];
    
    [self.manTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.manBriLabel);
        make.top.mas_equalTo(self.manBriLabel.mas_bottom).offset(30 * SCREENH6);
    }];
    
    [self.manNameText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.manNameLabel);
        make.leading.mas_equalTo(self.manNameLabel.mas_trailing);
        make.height.mas_equalTo(30 * SCREENH6);
        make.trailing.mas_equalTo(self.topView).offset(-15 * SCREENH6);
    }];
    
    [self.manBriText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.manBriLabel);
        make.trailing.leading.height.mas_equalTo(self.manNameText);
    }];
    
    [self.manTimeText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.manTimeLabel);
        make.trailing.leading.height.mas_equalTo(self.manBriText);
    }];
    
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.topView);
        make.top.mas_equalTo(self.topView.mas_bottom).offset(35 * SCREENH6);
    }];
    
    [self.womanIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.bottomView);
        make.width.height.leading.mas_equalTo(self.manIcon);
    }];
    
    [self.womanNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.womanIcon.mas_trailing).offset(10 * SCREENH6);
        make.top.mas_equalTo(self.bottomView).offset(20 * SCREENH6);
    }];
    
    [self.womanBriLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.womanNameLabel);
        make.top.mas_equalTo(self.womanNameLabel.mas_bottom).offset(30 * SCREENH6);
    }];
    
    [self.womanTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.womanBriLabel);
        make.top.mas_equalTo(self.womanBriLabel.mas_bottom).offset(30 * SCREENH6);
    }];
    
    [self.womanNameText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.womanNameLabel);
        make.trailing.leading.height.mas_equalTo(self.manTimeText);
    }];
    
    [self.womanBriText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.womanBriLabel);
        make.trailing.leading.height.mas_equalTo(self.womanNameText);
    }];
    
    [self.womanTimeText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.womanTimeLabel);
        make.trailing.leading.height.mas_equalTo(self.womanBriText);
    }];
    
    [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.bottomView.mas_bottom).offset(25 * SCREENH6);
        make.leading.mas_equalTo(self.view).offset(17.5 * SCREENH6);
        make.trailing.mas_equalTo(self.view).offset(-17.5 * SCREENH6);
        make.height.mas_equalTo(50 * SCREENH6);
    }];
}


- (UILabel *)setupLabelWith:(NSString *)str{
    
    UILabel * label = [UILabel new];
    
    label.text = str;
    label.textColor = ColorWithRGB(0xFDEB83);
    label.font = [UIFont systemFontOfSize:16 * SCREENH6];
    
    return label;
}


- (UITextField *)setupTextFiledWith:(NSString *)str{
    
    UITextField * textFiled = [[UITextField alloc] init];
    
    textFiled.borderStyle = UITextBorderStyleNone;
    textFiled.backgroundColor = [UIColor clearColor];
    //设置光标颜色
    textFiled.tintColor = [UIColor whiteColor];
    //修改占位符文字颜色
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = ColorWithRGB(0xCCCCCC);
    attrs[NSFontAttributeName] = [UIFont fontWithName:@"PingFangSC-Regular" size:17 * SCREENH6];
    textFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:str attributes:attrs];
    textFiled.textColor = ColorWithRGB(0xFDEB83);
    //    accountText.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 22 * SCREENH6, 0)];
    //    //设置显示模式为永远显示(默认不显示)
    //    accountText.leftViewMode = UITextFieldViewModeAlways;
    textFiled.font = [UIFont fontWithName:@"PingFangSC-Regular" size:17 * SCREENH6];
    
    return textFiled;
}

- (void)clickSureButton{
    
    NSString * str = [NSString stringWithFormat:@"/appstore/marriage/day?key=276cd36bb45f0&menDate=%@&menHour=%@&womanDate=%@&womanHour=%@",self.manBriText.text,self.manTimeText.text,self.womanBriText.text,self.womanTimeText.text];
    
    
    NSCharacterSet *encodeUrlSet = [NSCharacterSet URLQueryAllowedCharacterSet];
    NSString *encodeUrl = [str stringByAddingPercentEncodingWithAllowedCharacters:encodeUrlSet];
    
    [[MobNetworkingTool sharedTools] GET:encodeUrl parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
      
        
        NSLog(@"res====%@",responseObject);
        
        int retCode = [responseObject[@"retCode"] intValue];
        
        if (retCode == 200) {
            
            NSDictionary * result = responseObject[@"result"];
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"测试结果"
                                                                           message:result[@"marriageType"]
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      //响应事件
                                                                      [self.navigationController popViewControllerAnimated:YES];
                                                                  }];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"再试试" style:UIAlertActionStyleDefault
                                                                  handler:^(UIAlertAction * action) {
                                                                      //响应事件
                                                                      self.manNameText.text = @"";
                                                                      self.manBriText.text = @"";
                                                                      self.manTimeText.text = @"";
                                                                      self.womanNameText.text = @"";
                                                                      self.womanBriText.text = @"";
                                                                      self.womanTimeText.text = @"";
                                                                  }];
            
            [alert addAction:defaultAction];
            [alert addAction:cancleAction];
            [self presentViewController:alert animated:YES completion:nil];
        }else{
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"msg"
                                                                           message:responseObject[@""]
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      //响应事件
                                                                      [self.navigationController popViewControllerAnimated:YES];
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
            
        }
        
        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
